//
//  LotteryView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/23.
//

#import "LotteryView.h"
#import "CommonUtil.h"
#import "LotteryTypeView.h"

@interface LotteryView()

@property (assign, nonatomic) CGFloat  normalWidth;
@property (assign, nonatomic) CGFloat  selectWidth;
@property (assign, nonatomic) CGFloat  normalHeight;
@property (assign, nonatomic) CGFloat  selectHeight;
@property (assign, nonatomic) CGFloat  normalY;
@property (assign, nonatomic) CGFloat  selectY;

@property (strong, nonatomic) LotteryTypeView * gangcaiView;
@property (strong, nonatomic) LotteryTypeView * xinjiapocaiView;
@property (strong, nonatomic) LotteryTypeView * aocaiView;
@property (strong, nonatomic) LotteryTypeView * taiwancaiView;
@property (strong, nonatomic) LotteryTypeView * shishicaiView;

@property (strong, nonatomic) NSArray * lotteryViews;
@property (strong, nonatomic) LotteryTypeView * lastView;

@end

@implementation LotteryView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.selectY = 0;
        self.normalY = AD(15);
        self.normalWidth = (self.width-AD(40)-AD(10))/5;
        self.selectWidth = self.normalWidth+AD(10);
        self.normalHeight = self.height-self.normalY;
        self.selectHeight = self.height;

        
        self.lastView = self.gangcaiView;
        [self addSubview:self.gangcaiView];
        [self addSubview:self.xinjiapocaiView];
        [self addSubview:self.aocaiView];
        [self addSubview:self.taiwancaiView];
        [self addSubview:self.shishicaiView];
        
        self.lotteryViews = @[self.gangcaiView,self.xinjiapocaiView,self.aocaiView,self.taiwancaiView,self.shishicaiView];
        [self refreshIndex:0];
    }
    return self;
}

- (void)refreshIndex:(NSInteger)index{
    
    if (self.actionBlock) {
        self.actionBlock(index);
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        
        if (index == 0) {
            self.gangcaiView.frame = CGRectMake(0, self.selectY, self.selectWidth, self.selectHeight);
            self.xinjiapocaiView.frame = CGRectMake(self.gangcaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.aocaiView.frame = CGRectMake(self.xinjiapocaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.taiwancaiView.frame = CGRectMake(self.aocaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.shishicaiView.frame = CGRectMake(self.taiwancaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
        }else if (index == 1){
            self.gangcaiView.frame = CGRectMake(0, self.normalY, self.normalWidth, self.normalHeight);
            self.xinjiapocaiView.frame = CGRectMake(self.gangcaiView.right + AD(10), self.selectY, self.selectWidth, self.selectHeight);
            self.aocaiView.frame = CGRectMake(self.xinjiapocaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.taiwancaiView.frame = CGRectMake(self.aocaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.shishicaiView.frame = CGRectMake(self.taiwancaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
        }else if (index == 2){
            self.gangcaiView.frame = CGRectMake(0, self.normalY, self.normalWidth, self.normalHeight);
            self.xinjiapocaiView.frame = CGRectMake(self.gangcaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.aocaiView.frame = CGRectMake(self.xinjiapocaiView.right + AD(10), self.selectY, self.selectWidth, self.selectHeight);
            self.taiwancaiView.frame = CGRectMake(self.aocaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.shishicaiView.frame = CGRectMake(self.taiwancaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
        }else if (index == 3){
            self.gangcaiView.frame = CGRectMake(0, self.normalY, self.normalWidth, self.normalHeight);
            self.xinjiapocaiView.frame = CGRectMake(self.gangcaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.aocaiView.frame = CGRectMake(self.xinjiapocaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.taiwancaiView.frame = CGRectMake(self.aocaiView.right + AD(10), self.selectY, self.selectWidth, self.selectHeight);
            self.shishicaiView.frame = CGRectMake(self.taiwancaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
        }else if (index == 4){
            self.gangcaiView.frame = CGRectMake(0, self.normalY, self.normalWidth, self.normalHeight);
            self.xinjiapocaiView.frame = CGRectMake(self.gangcaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.aocaiView.frame = CGRectMake(self.xinjiapocaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.taiwancaiView.frame = CGRectMake(self.aocaiView.right + AD(10), self.normalY, self.normalWidth, self.normalHeight);
            self.shishicaiView.frame = CGRectMake(self.taiwancaiView.right + AD(10), self.selectY, self.selectWidth, self.selectHeight);
        }
        
        
        for (LotteryTypeView *view in self.lotteryViews) {

            if (view.tag == index) {
                view.selected = YES;
            }else{
                view.selected = false;
            }
            
        }
        
        
    }];
}

- (void)btnClick:(LotteryTypeView *)sender{
    [self refreshIndex:sender.tag];
}

-(LotteryTypeView *)gangcaiView{
    if (!_gangcaiView) {
        _gangcaiView = [[LotteryTypeView alloc] initWithFrame:CGRectMake(0, 0, self.selectWidth, self.selectHeight)];
        _gangcaiView.tag = 0;
        [_gangcaiView addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_gangcaiView setBackgroundImage:[UIImage imageNamed:@"港彩未选中"] forState:UIControlStateNormal];
        [_gangcaiView setBackgroundImage:[UIImage imageNamed:@"港彩未选中"] forState:UIControlStateHighlighted];
        [_gangcaiView setBackgroundImage:[UIImage imageNamed:@"香港"] forState:UIControlStateSelected];
        
    }
    return _gangcaiView;
}

-(LotteryTypeView *)xinjiapocaiView{
    if (!_xinjiapocaiView) {
        _xinjiapocaiView = [[LotteryTypeView alloc] initWithFrame:CGRectMake(_gangcaiView.right + AD(10), AD(15), self.normalWidth, self.normalHeight)];
        _xinjiapocaiView.tag = 1;
        [_xinjiapocaiView addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        [_xinjiapocaiView setBackgroundImage:[UIImage imageNamed:@"新加坡未选中"] forState:UIControlStateNormal];
        [_xinjiapocaiView setBackgroundImage:[UIImage imageNamed:@"新加坡未选中"] forState:UIControlStateHighlighted];
        [_xinjiapocaiView setBackgroundImage:[UIImage imageNamed:@"新加坡"] forState:UIControlStateSelected];
    }
    return _xinjiapocaiView;
}

-(LotteryTypeView *)aocaiView{
    if (!_aocaiView) {
        _aocaiView = [[LotteryTypeView alloc] initWithFrame:CGRectMake(_xinjiapocaiView.right + AD(10), AD(15), self.normalWidth, self.normalHeight)];
        _aocaiView.tag = 2;
        [_aocaiView addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        [_aocaiView setBackgroundImage:[UIImage imageNamed:@"澳彩未选中"] forState:UIControlStateNormal];
        [_aocaiView setBackgroundImage:[UIImage imageNamed:@"澳彩未选中"] forState:UIControlStateHighlighted];
        [_aocaiView setBackgroundImage:[UIImage imageNamed:@"澳门"] forState:UIControlStateSelected];
     
    }
    return _aocaiView;
}

-(LotteryTypeView *)taiwancaiView{
    if (!_taiwancaiView) {
        _taiwancaiView = [[LotteryTypeView alloc] initWithFrame:CGRectMake(_aocaiView.right + AD(10), AD(15), self.normalWidth, self.normalHeight)];
        _taiwancaiView.tag = 3;
        [_taiwancaiView addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_taiwancaiView setBackgroundImage:[UIImage imageNamed:@"台湾未选中"] forState:UIControlStateNormal];
        [_taiwancaiView setBackgroundImage:[UIImage imageNamed:@"台湾未选中"] forState:UIControlStateHighlighted];
        [_taiwancaiView setBackgroundImage:[UIImage imageNamed:@"台湾"] forState:UIControlStateSelected];
    }
    return _taiwancaiView;
}

-(LotteryTypeView *)shishicaiView{
    if (!_shishicaiView) {
        _shishicaiView = [[LotteryTypeView alloc] initWithFrame:CGRectMake(_taiwancaiView.right + AD(10), AD(15), self.normalWidth, self.normalHeight)];
        _shishicaiView.tag = 4;
        [_shishicaiView addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_shishicaiView setBackgroundImage:[UIImage imageNamed:@"时时彩未选中"] forState:UIControlStateNormal];
        [_shishicaiView setBackgroundImage:[UIImage imageNamed:@"时时彩未选中"] forState:UIControlStateHighlighted];
        [_shishicaiView setBackgroundImage:[UIImage imageNamed:@"时时彩"] forState:UIControlStateSelected];
    }
    return _shishicaiView;
}

@end
